﻿using CatUtils.Utils.Currency.IoUtils;
using System;
using System.Collections.Generic;
using System.Drawing;
using static CatUtils.Utils.Currency.PrintLog;

namespace CatUtilsDll.Utils.Currency.WebUtils
{/// <summary>
 /// 网络请求帮助
 /// </summary>
    public partial class WebHelper
    {   /// <summary>
        /// 判断图片是否存在，存在返回true
        /// </summary>
        /// <param name="ImgLink"></param>
        /// <returns></returns>
        public static bool CheckImgExits(string ImgLink)
        {
            return CheckImgExits(ImgLink, new List<BlockImgInfo> { new BlockImgInfo { FileSize = 0, ImgWidth = 0, ImgHeight = 0 } });
        }

        /// <summary>
        /// 需要被锁定的图片信息 通常填写404图片
        /// </summary>
        public class BlockImgInfo
        {
            /// <summary>
            /// 图片高度
            /// </summary>
            public int ImgHeight;

            /// <summary>
            /// 图片宽度
            /// </summary>
            public int ImgWidth;

            /// <summary>
            /// 图片大小
            /// </summary>
            public long FileSize;
        }

        /// <summary>
        /// 辅助获取被屏蔽图片的相关信息
        /// </summary>
        /// <param name="ImgLink"></param>
        /// <param name="PrintLog">是否打印日志</param>
        /// <returns></returns>
        public static BlockImgInfo GetImgInfo(string ImgLink, bool PrintLog = true)
        {
            BlockImgInfo imgInfo = new BlockImgInfo();
            string ImgSavePath = WebHelper.DownloadToFile(ImgLink, FileNameHelper.CreateTempFileName());

            try
            {
                long FileSize = FileHelper.GetFileSizeForByte(ImgSavePath);
                if (FileSize == 0)
                    return imgInfo;
                using (Image image = Image.FromFile(ImgSavePath))
                {
                    imgInfo = new BlockImgInfo { ImgHeight = image.Height, FileSize = FileSize, ImgWidth = image.Width };
                }
            }
            catch (Exception ex)
            {
                E(ex);
                return imgInfo;
            }
            finally
            {
                if (PrintLog)
                    Console.WriteLine($"链接：{ImgLink}，图片大小{imgInfo.FileSize}高度{imgInfo.ImgHeight}，宽度{imgInfo.ImgWidth}。");
                FileHelper.FileDelete(ImgSavePath);
            }

            return imgInfo;
        }

        /// <summary>
        ///判断图片是否存在，存在返回true
        /// </summary>
        /// <param name="ImgLink"></param>
        /// <param name="blockImgs"></param>
        /// <returns></returns>
        public static bool CheckImgExits(string ImgLink, List<BlockImgInfo> blockImgs)
        {
            string ImgSavePath = WebHelper.DownloadToFile(ImgLink, FileNameHelper.CreateTempFileName());

            try
            {
                long FileSize = FileHelper.GetFileSizeForByte(ImgSavePath);
                if (FileSize == 0)
                    return false;
                using (Image image = Image.FromFile(ImgSavePath))
                {
                    if (image.Width == 0 || image.Height == 0)
                        return false;
                    foreach (var item in blockImgs)
                    {
                        if (item.FileSize == FileSize && item.ImgHeight == image.Height && item.ImgWidth == image.Width)
                        {
                            Console.WriteLine($"链接：{ImgLink}失效，图片大小{item.FileSize}高度{item.ImgHeight}，宽度{item.ImgWidth}。");
                            return false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                E(ex);
                return false;
            }
            FileHelper.FileDelete(ImgSavePath);
            return true;
        }
    }
}